package com.akka.akkatry;

import com.simpleactors.Actor;
import com.simpleactors.ActorSystem;

/**
 * @author sad
 */
public class Main {

    private static class CounterActor extends Actor {
        long time = System.currentTimeMillis();
        int count = 0;

        public CounterActor() {
            super("first");
        }

        @Override
        public void receive(Object value, Actor receiver) {
            long temp = System.currentTimeMillis();
            if ((temp - time) > 1000) {
                time = temp;
                System.out.println(getName() + " Count = " + count);
                count = 0;
            }
            count++;
            counter2.send("some value");
        }
    }

    private static class Counter2Actor extends Actor {
        long time = System.currentTimeMillis();
        int count = 0;

        public Counter2Actor() {
            super("second");
        }

        @Override
        public void receive(Object value, Actor receiver) {
            long temp = System.currentTimeMillis();
            if ((temp - time) > 1000) {
                time = temp;
                System.out.println(getName() + " Count = " + count);
                count = 0;
            }
            count++;
            counter3.send("adslkjadlkjdsa");
        }
    }

    private static class Counter3Actor extends Actor {
        long time = System.currentTimeMillis();
        int count = 0;

        public Counter3Actor() {
            super("third");
        }

        @Override
        public void receive(Object value, Actor receiver) {
            long temp = System.currentTimeMillis();
            if ((temp - time) > 1000) {
                time = temp;
                System.out.println(getName() + " Count = " + count);
                count = 0;
            }
            count++;
        }
    }

    private static CounterActor counter1;
    private static Counter2Actor counter2;
    private static Counter3Actor counter3;

    public static void main(String[] args) throws InterruptedException {
        ActorSystem actorSystem = new ActorSystem();
        counter1 = new CounterActor();
        counter2 = new Counter2Actor();
        counter3 = new Counter3Actor();
        actorSystem.addActor(counter1).addActor(counter2).addActor(counter3);

        while (true) {
            counter1.send("some value");
        }
    }
}
